package cn.licoy.wdog.core.controller.hospital2.doctor;

import cn.licoy.wdog.common.bean.ResponseCode;
import cn.licoy.wdog.common.bean.ResponseResult;
import cn.licoy.wdog.core.config.jwt.JwtToken;
import cn.licoy.wdog.core.dto.hospital2.doctor.CertificationDto;
import cn.licoy.wdog.core.dto.hospital2.doctor.RandomDto;
import cn.licoy.wdog.core.dto.hospital2.doctor.SelectEarningsDto;
import cn.licoy.wdog.core.dto.hospitalUser.HospitalUserDto;
import cn.licoy.wdog.core.dto.hospitalUser.ReturnDigninDto;
import cn.licoy.wdog.core.entity.hospital.HospitalUser;
import cn.licoy.wdog.core.entity.hospital2.Certification;
import cn.licoy.wdog.core.entity.hospital2.Withdraw;
import cn.licoy.wdog.core.service.hospital.HospitalUserService;
import cn.licoy.wdog.core.service.hospital2.CertificationService;
import cn.licoy.wdog.core.service.hospital2.DoctorService;
import cn.licoy.wdog.core.service.hospital2.EarningsService;
import cn.licoy.wdog.core.service.hospital2.WithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author Sunne
 * @Date $time$ $date$
 * @Param $param$
 * @return $return$
 **/
/**
 *——————————————————————————女神保佑，永无bug——————————————————————
 *
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::  FUCK YOU
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */
@RestController
@RequestMapping(value = "/doctor")
@Api(tags = {"第二期--医生信息操作接口"})
public class DoctorController {
    @Autowired
    DoctorService doctorService;
    @Autowired
    HospitalUserService hospitalUserService;
    @Autowired
    CertificationService certificationService;
    @Autowired
    EarningsService earningsService;
    @Autowired
    WithdrawService withdrawService;
    @PostMapping(value = {"/registry"})
    @ApiOperation(value = "注册")
    public ResponseResult<ReturnDigninDto> saveDoctor(@RequestBody @Validated @ApiParam(value = "医生信息") HospitalUserDto userDto){
        HospitalUser user = hospitalUserService.add(userDto);
        ReturnDigninDto dto = new ReturnDigninDto();
        dto.setToken(((JwtToken)SecurityUtils.getSubject().getPrincipal()).getToken());
        dto.setUser(user);
        return ResponseResult.e(ResponseCode.OK,dto);
    }
    @PostMapping(value = {"/certification"})
    @ApiOperation(value = "医生实名认证")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public ResponseResult<CertificationDto> certification(@RequestBody @Validated @ApiParam(value = "认证资料") CertificationDto cidto){
      HospitalUser user =  hospitalUserService.getCurrentUser();
      if (user == null){
          return ResponseResult.e(ResponseCode.OK,"身份信息失效，请从新登录");
      }
     Certification certification =  certificationService.addCertification(cidto,user.getId());
     return ResponseResult.e(ResponseCode.OK,cidto);
    }

    @PostMapping(value = {"/getCertification"})
    @ApiOperation(value = "获取医生实名认证的图片")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public ResponseResult<ReturnDigninDto> getCertification(@RequestBody @Validated @ApiParam(value = "认证资料") String doctorId){

        return null;
    }
    @PostMapping(value = {"/getRandom"})
    @ApiOperation(value = "生成随机编号(不可用)")
    public ResponseResult<RandomDto> getRandom(){
        String heade = "E_";
        Date date = new Date();
        return null;
    }

    @PostMapping(value = {"/selectEarning"})
    @ApiOperation(value = "获取医生收益")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public ResponseResult<SelectEarningsDto> selectEarning(){
        HospitalUser user =  hospitalUserService.getCurrentUser();
        if (user == null){
            return ResponseResult.e(ResponseCode.OK,"身份信息失效，请从新登录");
        }
        SelectEarningsDto se = earningsService.selectEarning(user.getId());
        List<Withdraw> lw = withdrawService.selectWithdrawById(user.getId());
        if (se.getEarningsDtoList().size() == 0){
            return ResponseResult.e(ResponseCode.FAIL,"没有收益或者收益未到账");
        }
        if (lw.size() == 0){
            se.setObtainSum(Integer.toString(0));
            return ResponseResult.e(ResponseCode.OK,se);
        }
        int sum = 0;
        for (int i=0;i<lw.size();i++){
           sum =sum + Integer.parseInt(lw.get(i).getAmount());
        }
        //已经提现金额
        se.setObtainSum(Integer.toString(sum));
        return ResponseResult.e(ResponseCode.OK,se);
    }

}
